package controllers;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import play.Play;
import services.CaseService;
import services.CaseWordService;
import services.OperatorService;
import utils.BizConstants;
import vos.CaseSearchVo;
import vos.ReturnCode;
import vos.ReturnListVo;
import models.DrCase;
import models.DrVCase;
import models.DrWords;
import models.SystemOperator;

import com.github.pagehelper.PageInfo;

/**
 * 点播列表
 * @author wangshaoming
 *
 */
public class DemandController extends BaseController {
	/**
	 *开庭中列表
	 */
	public static void liveList(){
		//验证权限
		SystemOperator oper=getLoginUser();
		if(BizConstants.SERVICE_ROLE.equals(oper.getRoleId())){
			render("errors/404.html");
		}
		render();
	}
	/**
	 *已闭庭列表
	 */
	public static void stopList(){
		//验证权限
		SystemOperator oper=getLoginUser();
		if(BizConstants.SERVICE_ROLE.equals(oper.getRoleId())){
			render("errors/404.html");
		}
		render();
	}
	/**
	 * 获取案件列表
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param beginDate 开始时间
	 * @param endDate 结束时间
	 * @param searchsort 搜索条件类型
	 * @param searchText 搜索条件
	 */
	public static void getList(int page,int rows,String sort,String order,
			String beginDate,String endDate,String searchsort,String searchText,Integer status){
		SystemOperator operator=getLoginUser();
		long level=OperatorService.getLevelById(operator.getOperatorid());
		CaseSearchVo vo=new CaseSearchVo(beginDate,endDate,searchsort,searchText,status,new BigDecimal(level));
		PageInfo<DrVCase> cases=CaseService.getList(page,rows,sort,order,vo);
		renderJSON(new ReturnListVo(cases.getTotal(),cases.getList()));
	}
	/**
	 * 点播
	 * @param caseid 案件id
	 */
	public static void replay(String caseid){
		DrCase cases=CaseService.getById(caseid);
		if(cases==null){
			render("errors/404.html");
		}
		List<DrWords> words = CaseWordService.findCaseWordByCaseId(caseid);
		renderArgs.put("cases", cases);
		renderArgs.put("caseid", caseid);
		renderArgs.put("words", words);
		render();
	}
	/**
	 * 删除案件信息
	 */
	public static void deleteReplay(String caseid){
		if(StringUtils.isEmpty(caseid)){
			renderRespondJson(ReturnCode.ErrorCode, "请选择要删除的记录信息");
		}
		boolean isUpdate=CaseService.updateCaseStatus(BizConstants.CASE_STATUS_DELETE,caseid);
		if(!isUpdate){
			renderRespondJson(ReturnCode.ErrorCode, "删除记录失败");
		}
		renderRespondJson(ReturnCode.SuccessCode);
	}
	/**
	 * 下载音频
	 * @param caseid
	 */
	public static void downLoadReplay(String caseid){
		if(StringUtils.isEmpty(caseid)){
			render("errors/NotFound.html");
		}
		DrCase cases=CaseService.getById(caseid);
		if(cases==null||StringUtils.isEmpty(cases.getVideopath())){
			render("errors/NotFound.html");
		}
		File file=new File(Play.applicationPath +cases.getVideopath());
		if(!file.exists()){
			render("errors/NotFound.html");
		}
		renderBinary(file);
	}
}
